﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Transactions;
using Hangfire.Console;
using Hangfire.Dashboard.BasicAuthorization;
using Hangfire.HttpJob;
using Hangfire.MySql;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;

namespace Hangfire.HttpJobConsole
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddHangfire(Configuration);//Configuration是下面的方法
            //services.AddHangfire(r => r.UseSqlServerStorage("Data Source=.;Initial Catalog=HangfireDemo;User ID=sa;Password=LXMlxm123"));
        }

        /// <summary>
        /// server=#HOST#;Port=#PORT#;Database=#DATABASE#;User Id=#USER#;Password=#PASSWORD#;
        /// "Server=localhost;Port=3306;Database=hangfire;Uid=root;Pwd=abc123;charset=utf8;SslMode=none;Allow User Variables=True",
        /// </summary>
        /// <param name="globalConfiguration"></param>
        private void Configuration(IGlobalConfiguration globalConfiguration)
        {
            globalConfiguration.UseStorage(
                    new MySqlStorage(
                        "Server=localhost;Port=3306;Database=HangfireDemo;Uid=root;Pwd=abc123;charset=utf8;SslMode=none;Allow User Variables=True",
                        new MySqlStorageOptions
                        {
                            TransactionIsolationLevel = IsolationLevel.ReadCommitted,
                            QueuePollInterval = TimeSpan.FromSeconds(15),
                            JobExpirationCheckInterval = TimeSpan.FromHours(1),
                            CountersAggregateInterval = TimeSpan.FromMinutes(5),
                            PrepareSchemaIfNecessary = true,
                            DashboardJobListLimit = 50000,
                            TransactionTimeout = TimeSpan.FromMinutes(1),
                        }))
                .UseConsole()
                .UseHangfireHttpJob();

            //globalConfiguration.UseSqlServerStorage("Data Source=.;Initial Catalog=HangfireDemo;User ID=sa;Password=LXMlxm123")
            //    .UseConsole()
            //    .UseHangfireHttpJob();
        }


        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {

            app.UseHangfireServer();
            app.UseHangfireDashboard("/hangfire", new DashboardOptions
            {
                Authorization = new[] { new BasicAuthAuthorizationFilter(new BasicAuthAuthorizationFilterOptions
                {
                    RequireSsl = false,
                    SslRedirect = false,
                    LoginCaseSensitive = true,
                    Users = new []
                    {
                        new BasicAuthAuthorizationUser
                        {
                            Login = "admin",
                            PasswordClear =  "admin"
                        }
                    }

                }) }
            });

        }
    }
}
